package cn.edu.scau.cmi.chenmiaopeng.compositePattern.transparentPattern;
import java.util.ArrayList;
import java.util.List;

import cn.edu.scau.cmi.chenmiaopeng.controller.compositePattern.CompositePatternController;
import cn.edu.scau.cmi.chenmiaopeng.compositePattern.safePattern.Organization_S;
import cn.edu.scau.cmi.chenmiaopeng.compositePattern.safePattern.Team_S;
public class InquiryOrganization{
	//全局的organizations
   private static Team_S orginalOrg_S;
   private static Team_T orginalOrg_T;
   private Organization_S fatherOrg_S;  //初始节点(定义为team)
   private Organization_T fatherOrg_T;
   public InquiryOrganization() {
//	  this.orignalOrg = orignalOrg;
   }
   public static void setOrginalOrg_S(Team_S orginal_S) {
	   orginalOrg_S = orginal_S;
   }
   public static void setOrginalOrg_T(Team_T orginal_T) {
	   orginalOrg_T = orginal_T;
   }
   public Organization_T getFatherOrganization_T() {  //返回查询节点的父节点
	   return fatherOrg_T;
   }
   public Organization_T getOrganization_T(Organization_T now_org, String orgName) {  //Team 为 orignalOrg递归地查找节点，如果找到将其返回
	       if(orginalOrg_T.getName().equals(orgName)) {  //如果点击的节点即为根节点，则以根节点返回
	    	   return orginalOrg_T;
	       }
	       else return recursionSearchOrg_T(now_org,orgName);
   }
   private Organization_T recursionSearchOrg_T(Organization_T now_org, String orgName) {
	   fatherOrg_T = now_org;
	   ArrayList<Organization_T> organizationMember = ((Team_T)now_org).getOrganizationArrayList();
	   Organization_T nowFoundOrg = null;
       for(int i=0;i<organizationMember.size();i++) {
    	   if(organizationMember.get(i).getName().equals(orgName)) {
//    		   if(organizationMember.get(i) instanceof Personal) {
    			   return organizationMember.get(i);
//    		   }
    	   }
    	   else {
    		   if(organizationMember.get(i) instanceof Team_T) {
    			   fatherOrg_T = organizationMember.get(i);
    			   nowFoundOrg =  getOrganization_T((Team_T)organizationMember.get(i), orgName);
    		   }
    	   }
       }
     return nowFoundOrg;
   }
//   public Organization_S getFatherOrganization_S() {  //返回查询节点的父节点  编辑节点选择的就是父节点，不用再查询父节点
//	   return fatherOrg_S;
//   }
   public Organization_S getOrganization_S(Organization_S now_org, String orgName) {  //Team 为 orignalOrg递归地查找节点，如果找到将其返回
       if(orginalOrg_S.getName().equals(orgName)) {  //如果点击的节点即为根节点，则以根节点返回
    	   return orginalOrg_S;
       }
       else return recursionSearchOrg_S(now_org,orgName);
}
   private Organization_S recursionSearchOrg_S(Organization_S organization_S, String orgName) {  //Team 为 orignalOrg递归地查找节点，如果找到将其返回
	    fatherOrg_S = organization_S;
		   ArrayList<Organization_S> organizationMember = ((Team_S)organization_S).getOrganizationArrayList();
		   Organization_S nowFoundOrg = null;
	       for(int i=0;i<organizationMember.size();i++) {
	    	   if(organizationMember.get(i).getName().equals(orgName)) {
//	    		   if(organizationMember.get(i) instanceof Personal) {
	    			   return organizationMember.get(i);
//	    		   }
	    	   }
	    	   else {
	    		   if(organizationMember.get(i) instanceof Team_S) {
	    			   fatherOrg_S = organizationMember.get(i);
	    			   nowFoundOrg =  getOrganization_S((Team_S)organizationMember.get(i), orgName);
	    		   }
	    	   }
	       }
	     return nowFoundOrg;
	     }
}
